import Vue from "vue";
import SessionCache from "@/utils/cache";
import {
    accountLoginRequest
} from "@/service/login/login";
import router from "@/router";
import {
    Message
} from "element-ui";
import request from "@/service";

const loginModule = {
    namespaced: true,
    state() {
        return {
            token: "",
        };
    },

    mutations: {
        changeToken(state, token) {
            state.token = token;
        },
    },

    actions: {
        async accountLoginAction({
                                     commit
                                 }, payload) {
            // 1. 登录逻辑实现
            const res = await accountLoginRequest(payload);
            if (res.code !== 400) {
                SessionCache.setCache("IDInfo", res.data);
                const data = await request.post({
                    url: "/v1.RegisterUser/info",
                    data: {
                        jwt: SessionCache.getCache("IDInfo"),
                    },
                });
                if (data.code === 200) {
                    SessionCache.setCache("InfoMation", data.data);
                    router.push("/MyCourse");

                } else {
                    console.log(err);
                }
            } else {
                Message.error({
                    message: "账号或密码错误",
                });
            }
        },

        /**
         * 考试系统登录
         */
        async examLoginAction({
                                  commit
                              }, payload) {

            if (!payload.idcard) {
                return Message.error({
                    message: "请输入账号！",
                });
            }

            if (!payload.pwd) {
                return Message.error({
                    message: "请输入密码！",
                });
            }

            if (!payload.inputY) {
                return Message.error({
                    message: "请输入验证码！",
                });
            }

            if (payload.identifyCode !== payload.inputY) {
                return Message.error({
                    message: "验证码不正确！",
                });
            }

            // 1. 登录逻辑实现
            // 1.1 登录请求
            const res = await accountLoginRequest(payload);
            if (res.code !== 400) {
                SessionCache.setCache("IDInfo", res.data);

                /**
                 * 通过jwt 获取用户信息
                 * @type {*}
                 */
                const data = await request.post({
                    url: "/v1.RegisterUser/info",
                    data: {
                        jwt: SessionCache.getCache("IDInfo"),
                    },
                });
                if (data.code === 200) {
                    SessionCache.setCache("InfoMation", data.data);
                    router.push("/examListContent");
                } else {
                    Message.error({
                        message: "登录失败,请联系管理员！",
                    });
                }
            } else {
                Message.error({
                    message: "账号或密码错误",
                });
            }
        },

    },
};

export default loginModule;
